package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.InspectCollectCfgMapper;
import cc.rengu.igas.bomp.common.entity.InspectCollectCfg;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/6
 */
public class InspectCollectCfgMapperImpl implements InspectCollectCfgMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertInspectCollectCfg(InspectCollectCfg inspectCollectCfg) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertInspectCollectCfg");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectCollectCfg, null);
        if (0 != iReturnCode) {
            rglog.error("登记巡检采集配置信息失败,instId:<{}>,collectId:<{}>,iReturnCode:<{}>", inspectCollectCfg.getInstId(), inspectCollectCfg.getCollectId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateInspectCollectCfgById(InspectCollectCfg inspectCollectCfg) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateInspectCollectCfgById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectCollectCfg, null);
        if (0 != iReturnCode) {
            rglog.error("登记巡检采集配置信息失败,instId:<{}>,collectId:<{}>,iReturnCode:<{}>", inspectCollectCfg.getInstId(), inspectCollectCfg.getCollectId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public InspectCollectCfg selectInspectCollectCfgById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectInspectCollectCfgById");
        InspectCollectCfg inspectCollectCfg = new InspectCollectCfg();
        inspectCollectCfg.setInstId(instId);
        inspectCollectCfg.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectCollectCfg, inspectCollectCfg);
        if (0 != iReturnCode) {
            rglog.error("获取巡检采集配置信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return inspectCollectCfg;
    }

    @Override
    public InspectCollectCfg selectInspectCollectCfgByPrimaryKey(String instId, String collectId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectInspectCollectCfgByPrimaryKey");
        InspectCollectCfg inspectCollectCfg = new InspectCollectCfg();
        inspectCollectCfg.setInstId(instId);
        inspectCollectCfg.setCollectId(collectId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, inspectCollectCfg, inspectCollectCfg);
        if (0 != iReturnCode) {
            rglog.error("获取巡检采集配置信息失败,instId:<{}>,collectId:<{}>,iReturnCode:<{}>", instId, collectId, iReturnCode);
            return null;
        }
        return inspectCollectCfg;
    }

    @Override
    public List<InspectCollectCfg> selectInspectCollectCfgByInspectTypeAndMchntAttr(String instId, String inspectType, String mchntAttr) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectInspectCollectCfgByInspectTypeAndMchntAttr");
        InspectCollectCfg inspectCollectCfg = new InspectCollectCfg();
        inspectCollectCfg.setInstId(instId);
        inspectCollectCfg.setInspectType(inspectType);
        inspectCollectCfg.setMchntAttr(mchntAttr);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, inspectCollectCfg, InspectCollectCfg.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取巡检采集配置信息失败,instId:<{}>,inspectType:<{}>,mchntAttr:<{}>,", instId, inspectType, mchntAttr);
            return null;
        }
        return objectList.stream().map(item -> (InspectCollectCfg) item).collect(Collectors.toList());
    }
}
